﻿namespace System.ServiceModel.DomainServices.Client.ApplicationServices
{
    using System;

    public sealed class LoginOperation : AuthenticationOperation
    {
        private Action<LoginOperation> _completeAction;
        private readonly System.ServiceModel.DomainServices.Client.ApplicationServices.LoginParameters _loginParameters;

        internal LoginOperation(AuthenticationService service, System.ServiceModel.DomainServices.Client.ApplicationServices.LoginParameters loginParameters, Action<LoginOperation> completeAction, object userState) : base(service, userState)
        {
            this._loginParameters = loginParameters;
            this._completeAction = completeAction;
        }

        protected override IAsyncResult BeginCore(AsyncCallback callback)
        {
            return base.Service.BeginLogin(this.LoginParameters, callback, null);
        }

        protected override void CancelCore()
        {
            base.Service.CancelLogin(base.AsyncResult);
        }

        protected override object EndCore(IAsyncResult asyncResult)
        {
            return base.Service.EndLogin(asyncResult);
        }

        protected override void InvokeCompleteAction()
        {
            if (this._completeAction != null)
            {
                this._completeAction(this);
            }
        }

        protected override void RaiseCompletionPropertyChanges()
        {
            base.RaiseCompletionPropertyChanges();
            if (this.LoginSuccess)
            {
                base.RaisePropertyChanged("LoginSuccess");
            }
        }

        public System.ServiceModel.DomainServices.Client.ApplicationServices.LoginParameters LoginParameters
        {
            get
            {
                return this._loginParameters;
            }
        }

        public bool LoginSuccess
        {
            get
            {
                return ((this.Result != null) && this.Result.LoginSuccess);
            }
        }

        private LoginResult Result
        {
            get
            {
                return (LoginResult) base.Result;
            }
        }
    }
}

